/**
 * 
 */
package com.byzk.p2p.admin.borrow.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.byzk.p2p.admin.base.util.BaseUtil;
import com.byzk.p2p.admin.borrow.dao.BorrowDao;
import com.byzk.p2p.admin.borrow.dao.TenderDao;
import com.byzk.p2p.admin.borrow.dto.Borrow;
import com.byzk.p2p.admin.borrow.util.BorrowStatus;
import com.byzk.p2p.admin.borrow.vo.UserTenderVo;
import com.byzk.p2p.admin.fuiou.service.PreAuthCancelService;
import com.byzk.p2p.admin.money.dao.FundRecordDao;
import com.byzk.p2p.admin.money.dto.FundRecord;
import com.byzk.p2p.admin.money.util.FundRecordType;
import com.byzk.p2p.admin.score.dao.ScoreRecordDao;
import com.byzk.p2p.admin.score.dto.ScoreRecord;
import com.byzk.p2p.admin.score.util.ScoreRuleUtil;
import com.byzk.p2p.admin.user.dao.UserAccountDao;
import com.byzk.p2p.admin.user.dto.UserAccount;

/**
 * @Description:TODO
 * @author:Administrator
 * @time:2017年4月5日 上午9:55:27
 */
@Service
public class CancelService {

	@Autowired
	private TenderService tenderService;
	@Autowired
	private TenderDao tenderDao;
	@Autowired
	private UserAccountDao accountDao;
	@Autowired
	private FundRecordDao fundRecordDao;
	@Autowired
	private PreAuthCancelService preAuthCancelService;
	@Autowired
	private BorrowService borrowService;
	@Autowired
	private BorrowDao borrowDao;
	@Autowired
	private ScoreRecordDao scoreRecordDao;

	/**
	 * 获取一个标的所有投资记录
	 * 
	 * @param params
	 * @param index
	 * @param limit
	 * @return
	 */
	public List<UserTenderVo> listUserTenderByBorrowId(Integer borrowId) {
		return tenderDao.listUserTenderByBorrowId(borrowId);
	}

	/**
	 * Description: 更新投资用户资金 Company:xbd
	 * 
	 * @author:lilei
	 * @date:2017年4月5日上午10:48:39
	 */
	public boolean updateTenderUser(Integer borrowId, UserTenderVo tenderVo) {

		/** 首投记录 */
		FundRecord fundRecord = fundRecordDao.getFundRecordByObjectId(tenderVo.getId());
		UserAccount tenderAccount = accountDao.getByUserId(tenderVo.getUserId());
		Borrow borrow = borrowService.detailBorrow(borrowId);
		if (fundRecord != null) {
			tenderAccount.setStayAmount(tenderAccount.getStayAmount() - 20.0);
			
			/*清除首投奖励资金记录*/
			FundRecord record = new FundRecord();
			record.setUserId(tenderVo.getUserId());
			record.setObjectId(tenderVo.getId());
			record.setRemark("首投奖励");
			fundRecordDao.deleteFundRecord(record);
		}
		/** 累计投标超过10W奖励 **/
		Double sumTender = tenderService.sumMoney(tenderVo.getUserId(), borrowId);
		if (sumTender >= 100000.0) {
			tenderAccount.setStayAmount(tenderAccount.getStayAmount() - 200.0);

			/*清除投标奖励资金记录*/
			FundRecord record = new FundRecord();
			record.setUserId(tenderVo.getUserId());
			record.setObjectId(tenderVo.getId());
			record.setRemark("投标奖励");
			fundRecordDao.deleteFundRecord(record);
		}
		double amount = tenderVo.getAmount();// 投资金额
		/* 流标--借款标信息更新 */
		borrow.setTenderMoney(borrow.getTenderMoney() - amount);
		borrow.setNotTenderMoney(borrow.getNotTenderMoney() + amount);
		borrow.setStatus(BorrowStatus.BORROW);
		Double percent = borrow.getTenderMoney() / borrow.getAmount();
		int temp = (int) (percent * 10000);
		percent = (double) temp / 100;
		borrow.setSchedules(percent);
		borrowDao.updateBorrow(borrow);
		/* 流标--积分信息更新 */
		int count = 0;
		if ("MONTH".equals(borrow.getDateUtil())) {
			count = ScoreRuleUtil.calc(borrow.getDeadline(), amount);
		} else {
			if (borrow.getDeadline() >= 30) {
				int num = (int) borrow.getDeadline() / 30;
				count = ScoreRuleUtil.calc(num, amount);
			} else {
				count = (int) amount / 300;
			}
		}
		tenderAccount.setScore(tenderAccount.getScore() - count);

		if (count > 0) {
			ScoreRecord entity = new ScoreRecord();
			entity.setUserId(tenderVo.getUserId());
			entity.setOptType("out");
			entity.setAmount(count);
			entity.setWay("流标投标积分减");
			entity.setCreateTime(new Date());
			entity.setTerminal("PC");
			scoreRecordDao.insert(entity);
		}
		/* 流标--投资人账户更新 */
		tenderAccount.setBalance(tenderAccount.getBalance() + amount);
		tenderAccount.setFreezen(tenderAccount.getFreezen() - amount);
		Integer result1 = accountDao.update(tenderAccount);

		
		/*清除投资资金记录*/
		FundRecord record = new FundRecord();
		record.setUserId(tenderVo.getUserId());
		record.setObjectId(tenderVo.getId());
		record.setRemark("投资借款标");
		fundRecordDao.deleteFundRecord(record);
		
		
		/* 删除用户投资记录 */
		HashMap<String, Object> queryMap = new HashMap<String, Object>();
		queryMap.put("userId", tenderVo.getUserId());
		queryMap.put("borrowId", tenderVo.getBorrowId());
		queryMap.put("id", tenderVo.getId());
		Integer result = tenderDao.deleteTenderUser(queryMap);
		return (result > 0 && result1 > 0) ? true : false;
	}

}
